# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.


from web_lib.webutils import Response, expose , json , salvaInSessione, leggiInSessione , permalinkaTitle, pagination
from lib.utils import *
#from core.pages import *
from web_lib.page import Page

@expose('/pg/news_detail/<permalink>')
@expose('/news_detail/<permalink>')
def newsDetailByDenomination(req,subdomain=None, permalink=None):
    from dao.News import News
    news = News(req=req).select(permalink=permalink, active=True)
    ultimenews = News().select(batchSize=5, active=True)
    if news:

        mo = news[0]
        mo.clicks = int(mo.clicks or 0)+1
        mo.persist()
        if "/pg/" in req.path:
            f = "pg/news_detail"
            pg = "1"
        else:
            f = "main/news_detail"
            pg = "0"
        print " EFFET", f
        pageData = {'file' : f,
                    "pg" : pg,
                    "subdomain": subdomain,
                    "ultimenews":ultimenews,
                    "news":mo}
        return Page(req).render(pageData)
    else:
        pageData = {'file' : 'not_found'}
        return Page(req).render(pageData)

@expose('/pg/news_list')
@expose('/news_list')
def newss_list(req, static=None, subdomain=""):
    from dao.News import News
    #count = News().count(searchkey=req.form.get('searchkey'))
    batch = 10
    #args = pagination(req,batch,count)
    #args["page_list"] = "/news_list_home"
    newss = News().select(batchSize=batch,active=True)
    pageData = {'file' : 'main/news_list',
                #"subdomain": subdomain,
                #"args":args,
                "newss" :newss
                }
    return Page(req).render(pageData)
